<?php

namespace app\admin\controller\goodshandle;

use app\common\controller\Backend;
use app\admin\model\Attr;
use think\Db;
/**
 * 商品管理
 *
 * @icon fa fa-circle-o
 */
class Goods extends Backend
{
    
    /**
     * Goods模型对象
     * @var \app\admin\model\Goods
     */
    protected $model = null;
    protected $noNeedRight = ['getAttrData','getAttrInfo'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Goods');
        $this->view->assign("promTypeList", $this->model->getPromTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);


                    if ($result !== false)
                    {

                        //规格
                        $attr_id = $this->request->post('attr_id');
                        $attr_values = $this->request->post('attr_values/a');
                        // $attr_values = $_POST['attr_values'];

                        // $this->success('',null,[$attr_id,$attr_values]);
                        
                        if($attr_id && $attr_values){
                            foreach($attr_values as $k => $v){
                                Db::name('goods_attr')->insert([
                                    'goods_id' => $this->model->id,
                                    'attr_id' => $attr_id,
                                    'attr_value' => $v
                                ]);
                            }
                        }
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }



            $this->error(__('Parameter %s can not be empty', ''));
        }

        $this->assignconfig('isdialog',['data' => false]);
        $this->assignconfig('row',['data' => '']);
        $this->assignconfig('goodsattr',['data' => '']);
        $this->assignconfig('goodsattrData',['data' => '']);
        return $this->view->fetch();
    }


    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds))
        {
            if (!in_array($row[$this->dataLimitField], $adminIds))
            {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false)
                    {

                        //规格
                        $attr_id = $this->request->post('attr_id');
                        $attr_values = $this->request->post('attr_values/a');
                        // $attr_values = $_POST['attr_values'];

                        // $this->success('',null,[$attr_id,$attr_values]);
                        
                        if($attr_id && $attr_values){
                            Db::name('goods_attr')->where(['goods_id' => $ids])->delete();

                            foreach($attr_values as $k => $v){
                                Db::name('goods_attr')->insert([
                                    'goods_id' => $ids,
                                    'attr_id' => $attr_id,
                                    'attr_value' => $v
                                ]);
                            }
                        }
                        $this->success();
                    }
                    else
                    {
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $goodsattr = Db::name('goods_attr')->where(['goods_id' => $ids])->column('attr_value');

        $goodsattrId = Db::name('goods_attr')->where(['goods_id' => $ids])->value('attr_id');
        $attrStr = Db::name('attr')->where(['id' => $goodsattrId])->value('values');
        $attrValueData = explode(',',$attrStr);

        $attr = Db::name('attr')->select();
        // dump($goodsattr);exit;
        $this->view->assign("row", $row);
        $this->view->assign('attrData',$attrValueData);
        $this->view->assign('goodsattr',$attrValueData);
        $this->view->assign('attrId',$goodsattrId);
        $this->view->assign('attr',$attr);
        $this->assignconfig('row',['data' => $row]);
        $this->assignconfig('goodsattr',['data' => $goodsattr]);
        $this->assignconfig('goodsattrData',['data' => $goodsattrId]);
        $this->assignconfig('isdialog',['data' => 'true']);

        
        return $this->view->fetch();
    }

    public function getAttrData(){
        $category_id = $this->request->request('category_id');
        $attrM = model('attr');
         $data = $attrM->getAttrData($category_id);
        
        return json(['code'=>1,'msg'=>'','data' => $data]);
    }

    public function getAttrInfo(){
        $attr_id = $this->request->request('attr_id');
        $attr = Attr::get($attr_id);
        $data = explode(',', $attr['values']);
        return json(['code'=>1,'msg'=>'','data' => $data]);
    }

    public function selectpage(){
        return parent::selectpage();
    }


    
    

}
